<?php

namespace app\common\model;

use app\common\basic\BaseModel;

/**
 * 客户变更日志模型
 */
class CrmCustomerLogs extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';
    protected $table = 'vk_crm_customer_logs';
    
    protected $type = [
        'old_data' => 'json',
        'new_data' => 'json',
        'change_fields' => 'json',
        'created_at' => 'datetime',
    ];

    /**
     * 关联客户信息
     */
    public function customer()
    {
        return $this->belongsTo(CrmCustomers::class, 'customer_id', 'id')
            ->field(['id', 'name', 'mobile', 'company']);
    }

    /**
     * 关联操作人信息
     */
    public function operator()
    {
        return $this->belongsTo(SystemUser::class, 'operator_id', 'id')
            ->field(['id', 'nickname', 'username', 'avatar']);
    }
    
    public function getDescriptionAttr($value, $data)
    {
        return json_decode($value, true);
    }

    /**
     * 获取操作人类型描述
     */
    public function getOperatorRoleAttr($value, $data)
    {
        $types = [
            'admin' => '后台管理员',
            'api' => '前端用户',
        ];
        return $types[$data['operator_role']] ?? $data['operator_role'];
    }
}